using System;
using Activa.CoolStorage;

namespace Activa.ProMesh
{
	[MapTo("awtDynamicImage")]
	public abstract partial class DynamicImage : CSObject<DynamicImage,int>
	{
		public abstract int ImageID { get; }
		public abstract string ImageType { get; set; }
		[Lazy] public abstract byte[] ImageData { get; set; }
		public abstract DateTime CreationTime { get; set; }
	}

    partial class DynamicImage
    {
        public static byte[] GetDynamicImageData(int dynamicImageId, out string contentType)
        {
            DynamicImage img = Read(dynamicImageId);

            switch (img.ImageType)
            {
                case "JPG": contentType = "image/jpeg"; break;
                case "GIF": contentType = "image/gif"; break;
                default: contentType = "image/gif"; break;

            }

            CSDatabase.ExecuteNonQuery("delete awtDynamicImage where CreationTime < dateadd(n,-5,getdate())");

            return img.ImageData;
        }
    }
}
